<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <link rel="stylesheet" href="./css/common.css">
    <title>购物车</title>
</head>
<style>
    #shoppingCart {
        width: 800px;
        margin: 10px;
        padding: 10px;
        border: 1px solid #eeeeee;
    }

    #shoppingCart ul {
        padding: 5px 0;
    }

    li {
        width: 100px;
        /*border: 1px solid #99a9bf;*/
        text-align: center;
    }

    ul li:nth-child(2) {
        flex: 1
    }

    .jian, .jia, input {
        width: 20px;
    }

    .count {
        width: 30px;
        text-align: center;
    }

    .total {
        text-align: right;
    }

    [v-cloak] {
        display: none;
    }
</style>
<body>
<div id="app" v-cloak>
    
            <li>
                <button class="jian" @click="item.num--" :disabled="item.num<=0">-</button>
                <input type="text" value="1" :value="item.num" @blur="setNum(item.id,$event)">
                <button class="jia" @click="item.num++" :disabled="item.num>=10">+</button>
            </li>
            <li>{{item.price * item.num | us}}</li>

<script>
    new Vue({
        el: "#app",
        data() {
            return {
                isCheckAll: false,
                // 表示是否全选
                goodslist: [
                    {
                        id: 1,
                        name: "华为",
                        price: 3000,
                        num: "1",
                        isCheckAll: false
                    },
                    {
                        id: 2,
                        name: "苹果",
                        price: 10000,
                        num: "1",
                        isCheckAll: false
                    },
                    {
                        id: 3,
                        name: "塞班",
                        price: 10000,
                        num: "1",
                        isCheckAll: false
                    }]
            }
        },
        methods: {
            checkAll() {
                this.goodslist.forEach(r => r.isCheckAll = this.isCheckAll)
            },
            allcheck() {
            
                this.isCheckAll = this.goodslist.every(r => {
                    return r.isCheckAll
                })
            setNum(id, e) 
                let num = e.target.value;
                if (isNaN(num)) {
                    num = 1
                } // num的类型为isNaN设置num为1
                if (num < 1) {
                    num = 1
                } // num是Number类型且小于1 设置为1
                if (num > 10) {
                    num = 10
                }
                if (!Number.isInteger(num)) {
                    num = Math.floor(num)
                } // num是Nubmer类型但是是浮点型需要向下取整
                // 如果以上都不执行 这时候num为string类型
                //     需要强制转换
                num = parseInt(num);
                console.log(num);
                // 根据id 找到对应的商品对象
                let goods = this.goodslist.find(r => r.id === id);
                e.target.value = num;
                goods.num = num; // 再把对应商品的值赋值为num
            },
            del(id) {
                this.goodslist = this.goodslist.filter(r => r.id !== id)
                // //    把id不等于的删选出来，那么 id等于的就删除了
                // let index = this.goodslist.findIndex(r => r.id)
                // //    返回id是我们所点击内容所对象的列表的索引值
                // this.goodslist.splice(index, 1)
                // //    删除按照索引值为index的一个元素 ， 1 指删除的步长
            },
        },

    })
</script>
</body>
</html>